package context;

import leeks.FundBean;
import leeks.StockBean;

import java.util.HashSet;
import java.util.Set;

/**
 * 功能描述:
 *
 * @Author: luobo
 * @Date: 2021/2/8 0008 15:33:19
 */

public class Context{

    public void setStockBeanSet(Set<StockBean> stockBeanSet){
        this.stockBeanSet = stockBeanSet;
    }

    public Set<StockBean> getStockBeanSet(){
        return stockBeanSet;
    }

    private Set<StockBean> stockBeanSet = new HashSet<>();

    private Set<FundBean> fundBeanSet = new HashSet<>();

    private static class ContextHolder{
        /**
         * 静态初始化器，由JVM来保证线程安全
         */
        private static final Context instance = new Context();
    }

    public static Context getInstance(){
        return ContextHolder.instance;
    }

    private Context(){

    }

    public boolean add(StockBean stockBean){
        Set<StockBean> set = getInstance().getStockBeanSet();
        if (!set.add(stockBean)) {
            set.remove(stockBean);
            return set.add(stockBean);
        }
        return false;
    }

    public boolean add(FundBean fundBean){
        Set<FundBean> set = getInstance().getFundBeanSet();
        if (!set.add(fundBean)) {
            set.remove(fundBean);
            return set.add(fundBean);
        }
        return false;
    }


    public Set<FundBean> getFundBeanSet(){
        return fundBeanSet;
    }

    public void setFundBeanSet(Set<FundBean> fundBeanSet){
        this.fundBeanSet = fundBeanSet;
    }

}